//
//  EPMAddressViewController.m
//  myXianfeng
//
//  Created by 马继鵬 on 17/4/26.
//  Copyright © 2017年 EPM. All rights reserved.
//

#import "EPMAddressViewController.h"
#import "EPMAddressLocationCell.h"
#import "EPMLoginViewController.h"
#import "EPMAddressEditViewController.h"


@interface EPMAddressViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak,nonatomic) UIView* logView;
@property (weak,nonatomic) UITableView* tableView;
@end

@implementation EPMAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addtableView];
    [self addLogView];

    if (_LoginMgr.isLogin){
        self.logView.hidden = YES;
    }else{
        self.logView.hidden = NO;
    }
    
    self.navigationItem.title = @"地址管理";
}

#pragma mark - 添加tabelView -
- (void)addtableView{
    UITableView* tableView = [[UITableView alloc]initWithFrame:CGScreenFrame style:UITableViewStyleGrouped];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    tableView.dataSource = self;
    tableView.delegate = self;
    
    tableView.rowHeight = 60;
    
    [tableView registerNib:[UINib nibWithNibName:@"EPMAddressLocationCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"location"];
    
    [self addBottomView];
}
#pragma mark - 底部视图 -

- (void)addBottomView{
    UIView* BottomView = [[UIView alloc]init];
    [self.view insertSubview:BottomView aboveSubview:self.tableView];
    [BottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self.view);
        make.height.mas_equalTo(64);
    }];
    
    
    UIButton* addBtn = [[UIButton alloc]init];
    [BottomView addSubview:addBtn];
    [addBtn setTitle:@"+  新增地址" forState:UIControlStateNormal];
    [addBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    addBtn.backgroundColor = appColor;
    
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(BottomView);
        make.width.equalTo(BottomView).multipliedBy(2.0/3);
        make.height.mas_equalTo(40);
    }];
    [addBtn addTarget:self action:@selector(actionAddAddress:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 点击事件 -

- (void)actionAddAddress:(UIButton*)sender{
    
}


- (void)addLogView{
    UIView* logView = [[UIView alloc]init];
    _logView = logView;
    logView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:logView];
    [logView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.width.height.mas_equalTo(ScreenWidth);
    }];
    
    
    //设置登录按钮
    UIButton* logBtn = [[UIButton alloc]init];
    [logView addSubview:logBtn];
    [logBtn setTitle:@"登录" forState:UIControlStateNormal];
    [logBtn setBackgroundColor:appColor];
    [logBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    logBtn.layer.masksToBounds = YES;
    logBtn.layer.cornerRadius = 10;
    
    //点击跳转事件
    [logBtn addTarget:self action:@selector(logBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [logBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(logView);
        make.width.mas_equalTo(260);
        make.height.mas_equalTo(40);
    }];
    
    //
    UILabel* label = [[UILabel alloc]init];
    [logView addSubview:label];
    label.textAlignment = NSTextAlignmentCenter;
    [label sizeToFit];
    label.text = @"查看已有配送地址请先登录";
    label.font = [UIFont systemFontOfSize:13];
    label.textColor = [UIColor lightGrayColor];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(logView);
        make.top.equalTo(logBtn.mas_bottom).offset(7);
    }];
}

#pragma mark - 点击事件 -

- (void)logBtnClick:(id)sender{
    EPMLoginViewController* VC = [[EPMLoginViewController alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
}


#pragma mark - 数据源方法 -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0){
        EPMAddressLocationCell* cell = [tableView dequeueReusableCellWithIdentifier:@"location" forIndexPath:indexPath];
        [cell setEditBtnClick:^{
            EPMAddressEditViewController* VC = [EPMAddressEditViewController new];
            [self.navigationController pushViewController:VC animated:YES];
        }];
        
        return cell;
    }
    return [UITableViewCell new]; // Do any additional setup after loading the view.
}

#pragma mark - 代理方法 -



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
